﻿using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

// Token: 0x02000061 RID: 97
[RequireComponent(typeof(Camera))]
public class ScreenShotCamera : MonoBehaviour
{
	// Token: 0x06000317 RID: 791 RVA: 0x00014444 File Offset: 0x00012644
	private void Awake()
	{
		this.camera = base.GetComponent<Camera>();
		this.audio = base.GetComponent<AudioSource>();
	}

	// Token: 0x06000318 RID: 792 RVA: 0x00014460 File Offset: 0x00012660
	public void Shot(Texture tex)
	{
		this.CreateRenderTex();
		this.rawImage.texture = tex;
		this.deadStep = 1;
		this.Volume();
		this.audio.Play();
	}

	// Token: 0x06000319 RID: 793 RVA: 0x0001448C File Offset: 0x0001268C
	private void LateUpdate()
	{
		this.Volume();
		if (this.deadStep == 0)
		{
			RenderTexture.active = this.rendTex;
			Texture2D texture2D = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
			texture2D.filterMode = FilterMode.Point;
			texture2D.wrapMode = TextureWrapMode.Clamp;
			texture2D.ReadPixels(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height), 0, 0, false);
			texture2D.Apply();
			byte[] buffer = texture2D.EncodeToPNG();
			string str = string.Concat(new string[]
			{
				DateTime.Now.Year.ToString("0000"),
				DateTime.Now.Month.ToString("00"),
				DateTime.Now.Day.ToString("00"),
				DateTime.Now.Hour.ToString("00"),
				DateTime.Now.Minute.ToString("00"),
				DateTime.Now.Second.ToString("00"),
				DateTime.Now.Millisecond.ToString("000")
			});
			string path = this.directoryPath + "/" + str + ".png";
			if (!Directory.Exists(this.directoryPath))
			{
				Directory.CreateDirectory(this.directoryPath);
			}
			FileStream fileStream = new FileStream(path, FileMode.Create);
			BinaryWriter binaryWriter = new BinaryWriter(fileStream);
			binaryWriter.Write(buffer);
			fileStream.Close();
			this.camera.targetTexture = null;
		}
		if (this.deadStep <= 0 && this.AudioEnd())
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		this.deadStep--;
	}

	// Token: 0x0600031A RID: 794 RVA: 0x0001467C File Offset: 0x0001287C
	private void Destroy()
	{
		if (this.rendTex)
		{
			this.camera.targetTexture = null;
			this.rendTex.Release();
			UnityEngine.Object.Destroy(this.rendTex);
			this.rendTex = null;
		}
	}

	// Token: 0x0600031B RID: 795 RVA: 0x000146B8 File Offset: 0x000128B8
	private void CreateRenderTex()
	{
		if (this.rendTex != null)
		{
			this.camera.targetTexture = null;
			this.rendTex.Release();
			UnityEngine.Object.Destroy(this.rendTex);
		}
		this.rendTex = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
		this.rendTex.filterMode = FilterMode.Point;
		this.camera.orthographicSize = (float)(Screen.height / 2);
		this.camera.targetTexture = this.rendTex;
	}

	// Token: 0x0600031C RID: 796 RVA: 0x00014740 File Offset: 0x00012940
	private void Volume()
	{
		if (this.audio != null)
		{
			this.audio.volume = ConfigData.VolumeSoundEffect();
		}
	}

	// Token: 0x0600031D RID: 797 RVA: 0x00014764 File Offset: 0x00012964
	private bool AudioEnd()
	{
		return this.audio == null || (this.audio.clip.loadState == AudioDataLoadState.Loaded && !this.audio.isPlaying);
	}

	// Token: 0x040002CA RID: 714
	[SerializeField]
	private string directoryPath = "UserData/Cap";

	// Token: 0x040002CB RID: 715
	private RenderTexture rendTex;

	// Token: 0x040002CC RID: 716
	private int deadStep = 1;

	// Token: 0x040002CD RID: 717
	private Camera camera;

	// Token: 0x040002CE RID: 718
	public RawImage rawImage;

	// Token: 0x040002CF RID: 719
	private AudioSource audio;
}
